package net.child.chess.puzzles;

import net.child.chess.core.Board;
import net.child.chess.core.BoardParams;
import net.child.chess.pieces.King;
import net.child.chess.pieces.Queen;
import net.child.chess.utils.ChessDef;
import java.util.Random;

//	The game is King against King+Queen
public class MateByQueen implements IPuzzle
{
	public MateByQueen()
	{
		mBoardParams = new BoardParams(BoardParams.INFINTE_MOVES, ChessDef.Color.WHITE, ChessDef.Color.BLACK);
	}
	
	@Override
	public String name()
	{
		return "Mate By Queen";
	}
	
	//	Need to select random positions for Kings/Queen
	@Override
	public void randomize(Board board)
	{
		board.reset(mBoardParams);
		Random random = new Random();
		//	get random integer>=0 and less that NUM_PIECES
		int blackKingPosition = random.nextInt(ChessDef.NUM_PIECES);
		board.add(new King(ChessDef.Color.BLACK, blackKingPosition));
		int blackQueenPosition = random.nextInt(ChessDef.NUM_PIECES);
		for(int position = blackQueenPosition, i=0; i<ChessDef.NUM_PIECES; ++i)
		{
			if(board.piece(position) == null)
			{
				board.add(new Queen(ChessDef.Color.BLACK, position));
				break;
			}
			position = (++position) % ChessDef.NUM_PIECES;
		}
		int whiteKingPosition = random.nextInt(ChessDef.NUM_PIECES);
		for(int position = whiteKingPosition, i=0; i<ChessDef.NUM_PIECES; ++i)
		{
			if(board.piece(position) == null)
			{
				King king = new King(ChessDef.Color.WHITE, position); 
				board.add(king);
				if(board.hasValidMoves(position) && (board.isKingUnderAttack(ChessDef.Color.WHITE) == false))
				{
					break;
				}
				board.remove(king);
			}
			position = (++position) % ChessDef.NUM_PIECES;
		}
	}
	
	private BoardParams mBoardParams;
}
